
package com.huawei.housekeeper.config.datasource;

import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * flyway加载数据源完成迁移
 *
 * @author lWX1128557
 * @since 2022-03-10
 */
@Component
@RefreshScope
public class DataSourceConfiguration {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.url}")
    private String url;

    /**
     * flyway数据库脚本迁移
     */
    public void migration() {
        // 加载schema
        List<String> schemas = dataSourceProperties.getSchemas();
        // 脚本迁移
        schemas.stream()
            .forEach(schema -> Flyway.configure().dataSource(url, username, password).schemas(schema).load().migrate());
    }
}
